import yagmail


class EmailClient(object):
    def __init__(self, account, password, host, mail_from=None):
        self.user = account if not mail_from else {account: mail_from}
        self.password = password
        self.host = host

    def e_send_email(self, to, subject, contents, cc=None, bcc=None, attachments=None):
        """
        Send an email using yagmail.

        :param to: 收件人，可以是一个(str)或多个(list(str))
        :param subject: 邮件主题
        :param contents: 邮件内容，可以是字符串或者HTML
        :param cc: 抄送人员，可以是一个(str)或多个(list(str))
        :param bcc: 密送人员，可以是一个(str)或多个(list(str))
        :param attachments: 附件，可以是一个或多个
        """
        try:
            # 使用yagmail的上下文管理器功能，确保SMTP连接被正确关闭
            with yagmail.SMTP(self.user, self.password, self.host) as yag:
                yag.send(to=to, subject=subject, contents=contents,
                         cc=cc, bcc=bcc, attachments=attachments)
        except Exception as e:
            # 更具体的错误处理可以在这里进行
            raise e